/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.entity;

import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.cef.api.dataType.entity.ICefEntity;
import com.inspur.edp.cef.api.dataType.entity.ICefRootEntity;
import com.inspur.edp.cef.core.datatype.CefEntity;
import com.inspur.edp.cef.entity.changeset.IChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import lombok.var;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Objects;

public class ViewObjectEntity extends CefEntity implements ICefRootEntity {


    private String id;

    public ViewObjectEntity(String dataId) {
        this.id = dataId;
    }

    @Override
    public void modify(IChangeDetail change) {
        throw new NotImplementedException();
    }

    @Override
    public ICefEntity getChildEntity(String code, String id) {
        Objects.requireNonNull(code);

        if (extList != null) {
            for (var ext : extList) {
                var addedChild = ext.getAddedChilds();
                if (addedChild == null || addedChild.isEmpty()) {
                    continue;
                }
                var child = addedChild.stream().filter(item -> code.equals(item.getNodeCode())).findFirst();
                if (child.isPresent()) {
                    return child.get().createEntity(id);
                }
            }
        }

        var child = (ViewObjectEntity) innerGetChildEntity(code, id);
        //TODO: 增加从从表暂不支持
//        if (extList != null) {
//            child.setExtend(
//                extList.stream().map(item -> item.getChildEntity(code))
//                    .filter(item -> item != null)
//                    .collect(Collectors.toList()));
//        }
        return child;
    }

    protected ICefEntity innerGetChildEntity(String code, String id) {
        throw new BffFrameworkException(ErrorCodes.BFF_FRAMEWORK_1021);
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public IEntityData retrieveData() {
        throw new NotImplementedException();
    }

}
